<?php

namespace App\Modules\Admin\Services\Expenses;

use App\Exceptions\Exception;
use App\Modules\Admin\Entities\Expenses\CouponReceiveModel;
use App\Modules\Admin\Services\BaseService;
use Illuminate\Support\Facades\DB;

class CouponReceiveService extends BaseService
{
    public function __construct(CouponReceiveModel $master)
    {
        $this->model = $master;

        $this->with=['master_info','water_device_info'];
        $this->detailWith=['master_info','water_device_info'];
    }

    public function lists(array $params): array
    {
        $params['where_callback'] = function($query) use ($params){
            $request = request();
            // 按照名称进行搜索
            if (!empty($search = $request->input('search', ''))){
                $query->where('meter_name', 'LIKE', '%' . trim($search) . '%');
            } // apparatus_id
            if (!empty($community_id = $request->input('community_id', ''))){
                $query->where('community_id', $community_id);
            }
            if (!empty($apparatus_id = $request->input('apparatus_id', ''))){
                $query->where('apparatus_id', $apparatus_id);
            }

            if (!empty($master_id = $request->input('master_id', ''))){
                $query->where('master_id', $master_id);
            }
            if (!empty($type = $request->input('type', ''))){
                $query->where('type', $type);
            }

            // 是否公开
            $is_public = $request->input('is_hidden', -1);
            if ($is_public > -1){
                $query->where('is_hidden', '=', $is_public);
            }
        };
        return parent::lists($params); // TODO: Change the autogenerated stub
    }

    public function getSelectLists($request)
    {
        return $this->model->where(function($query) use ($request){
            $search = $request->input('search', '');
            // 可以按照名称进行搜索
            if (!empty($search)){
                $query->where('master_name', 'LIKE', '%' . trim($search) . '%');
            }

            if (!empty($apparatus_id = $request->input('apparatus_id', ''))){
                $query->where('apparatus_id', $apparatus_id);
            }
        })->orderBy($this->model->getKeyName(), 'ASC')->get(['master_id','master_name','master_sn'])->toArray();
    }
    public function getSelectDetail($params)
    {
        $master_id = $params['master_id'];
        return $this->model->where(['master_id'=>$master_id])->orderBy($this->model->getKeyName(), 'ASC')->first();
    }

    public function create(array $params)
    {
        [$start_day,$end_day] = $params['status_end_day'];
        unset($params['status_end_day']);
        unset($params['end_day']);
        $params['start_day'] = $start_day;
        $params['end_day'] = $end_day;
//        exit(json_encode([date('',$status_day),date('',s)$end_day]));
        DB::beginTransaction();
        try{
            parent::create($params); // TODO: Change the autogenerated stub
            DB::commit();
            return true;
        }catch (Exception $e){
            DB::rollBack();
            throw new Exception($e->getMessage());
        }
    }

    public function update(array $params)
    {
        DB::beginTransaction();
        try{
            parent::update($params); // TODO: Change the autogenerated stub

            DB::commit();
            return true;
        }catch (Exception $e){
            DB::rollBack();
            throw new Exception($e->getMessage());
        }
    }



    public function delete(array $params)
    {
        $primaryKey = $this->model->getKeyName();
        if (isset($params[$primaryKey]) && $params[$primaryKey] == 1){
            throw new Exception('超管禁止删除！');
        }
        // 如果是批量删除，那么移除Id
        if(isset($params['is_batch']) && $params['is_batch'] == 1){
            $ids_ary = explode(',', $params[$primaryKey]);
            if (in_array(1, $ids_ary)){
                $key = array_search(1, $ids_ary);
                unset($ids_ary[$key]);
                $params[$primaryKey] = implode(',', $ids_ary);
            }
        }
        return parent::delete($params); // TODO: Change the autogenerated stub
    }
}
